import axios from 'axios'
import qs from 'qs'
import { URL } from './url'

axios.defaults.withCredentials = true
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'

export default async function (url = '', data = {}, type = 'POST', urlAll = null, config = {} ) {

	url =  URL + url;

	if(urlAll === null) data = qs.stringify(data);
	type = type.toUpperCase()



	var ajax = type == 'POST' ? axios.post(url, data,config) : axios.get(url, data,config);

	ajax = ajax.then(res => res.data)
		.then( res => {
			if(res.code == 102){
				alert('请先登录！')
				window.location.href = URL + '/mobile/#/user/login'
			}else if(res.code != 2){
				alert(res.msg)
			}else{
				return res
			}
		})
		.catch(e => {
			console.log('报错了，快去看看代码吧！ -- ' + e)
			window.location.href = '/#/user/login'
		});



	return ajax
}
